package cn.stylefeng.guns.modular.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.biz.entity.WmInventoryQty;
import cn.stylefeng.guns.modular.biz.enums.WmInventoryQtyExceptionEnum;
import cn.stylefeng.guns.modular.biz.mapper.WmInventoryQtyMapper;
import cn.stylefeng.guns.modular.biz.pojo.request.WmInventoryQtyRequest;
import cn.stylefeng.guns.modular.biz.service.WmInventoryQtyService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;
import java.math.BigDecimal;

/**
 * 煤场库存（按批次/量管理）业务实现层
 *
 * @author 张朋飞
 * @since 2024/09/13 16:37
 */
@Service
public class WmInventoryQtyServiceImpl extends ServiceImpl<WmInventoryQtyMapper, WmInventoryQty> implements WmInventoryQtyService {

	@Override
    public void add(WmInventoryQtyRequest wmInventoryQtyRequest) {
        WmInventoryQty wmInventoryQty = new WmInventoryQty();
        BeanUtil.copyProperties(wmInventoryQtyRequest, wmInventoryQty);
        this.save(wmInventoryQty);
    }

    @Override
    public void del(WmInventoryQtyRequest wmInventoryQtyRequest) {
        WmInventoryQty wmInventoryQty = this.queryWmInventoryQty(wmInventoryQtyRequest);
        this.removeById(wmInventoryQty.getWiqId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(WmInventoryQtyRequest wmInventoryQtyRequest) {
        this.removeByIds(wmInventoryQtyRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(WmInventoryQtyRequest wmInventoryQtyRequest) {
        WmInventoryQty wmInventoryQty = this.queryWmInventoryQty(wmInventoryQtyRequest);
        BeanUtil.copyProperties(wmInventoryQtyRequest, wmInventoryQty);
        this.updateById(wmInventoryQty);
    }

    @Override
    public WmInventoryQty detail(WmInventoryQtyRequest wmInventoryQtyRequest) {
        return this.queryWmInventoryQty(wmInventoryQtyRequest);
    }

    @Override
    public PageResult<WmInventoryQty> findPage(WmInventoryQtyRequest wmInventoryQtyRequest) {
        LambdaQueryWrapper<WmInventoryQty> wrapper = createWrapper(wmInventoryQtyRequest);
        Page<WmInventoryQty> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<WmInventoryQty> findList(WmInventoryQtyRequest wmInventoryQtyRequest) {
        LambdaQueryWrapper<WmInventoryQty> wrapper = this.createWrapper(wmInventoryQtyRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 张朋飞
     * @since 2024/09/13 16:37
     */
    private WmInventoryQty queryWmInventoryQty(WmInventoryQtyRequest wmInventoryQtyRequest) {
        WmInventoryQty wmInventoryQty = this.getById(wmInventoryQtyRequest.getWiqId());
        if (ObjectUtil.isEmpty(wmInventoryQty)) {
            throw new ServiceException(WmInventoryQtyExceptionEnum.WM_INVENTORY_QTY_NOT_EXISTED);
        }
        return wmInventoryQty;
    }

    /**
     * 创建查询wrapper
     *
     * @author 张朋飞
     * @since 2024/09/13 16:37
     */
    private LambdaQueryWrapper<WmInventoryQty> createWrapper(WmInventoryQtyRequest wmInventoryQtyRequest) {
        LambdaQueryWrapper<WmInventoryQty> queryWrapper = new LambdaQueryWrapper<>();

        Long wiqId = wmInventoryQtyRequest.getWiqId();
        queryWrapper.eq(ObjectUtil.isNotNull(wiqId), WmInventoryQty::getWiqId, wiqId);

        String wiqName = wmInventoryQtyRequest.getWiqName();
        queryWrapper.like(ObjectUtil.isNotEmpty(wiqName), WmInventoryQty::getWiqName, wiqName);

        String wiqCode = wmInventoryQtyRequest.getWiqCode();
        queryWrapper.like(ObjectUtil.isNotEmpty(wiqCode), WmInventoryQty::getWiqCode, wiqCode);

        String wiqManagerCode = wmInventoryQtyRequest.getWiqManagerCode();
        queryWrapper.like(ObjectUtil.isNotEmpty(wiqManagerCode), WmInventoryQty::getWiqManagerCode, wiqManagerCode);

        String wiqManagerHeightCode = wmInventoryQtyRequest.getWiqManagerHeightCode();
        queryWrapper.like(ObjectUtil.isNotEmpty(wiqManagerHeightCode), WmInventoryQty::getWiqManagerHeightCode, wiqManagerHeightCode);

        BigDecimal wiqMass = wmInventoryQtyRequest.getWiqMass();
        queryWrapper.eq(ObjectUtil.isNotNull(wiqMass), WmInventoryQty::getWiqMass, wiqMass);

        String remark = wmInventoryQtyRequest.getRemark();
        queryWrapper.like(ObjectUtil.isNotEmpty(remark), WmInventoryQty::getRemark, remark);


        return queryWrapper;
    }

}
